
package com.jf.cloud.payment.controller;

import com.alipay.api.AlipayApiException;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.jf.cloud.common.constant.PayType;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.payment.bo.PayInfoResultBO;
import com.jf.cloud.payment.constant.PayEntry;
import com.jf.cloud.payment.constant.PayStatus;
import com.jf.cloud.payment.manager.PayManager;
import com.jf.cloud.payment.manager.PayNoticeManager;
import com.jf.cloud.payment.model.PayInfo;
import com.jf.cloud.payment.service.PayInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Hidden;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Objects;

/**
 * @author zz
 */
@Hidden
@RestController
@RequestMapping("/ua/notice/pay")
public class PayNoticeController {

    @Autowired
    private PayInfoService payInfoService;

    @Autowired
    private PayManager payManager;


    @Autowired
    private PayNoticeManager payNoticeManager;
    /**
     * 支付异步回调
     */
    @RequestMapping("/{payEntry}/{payType}")
    public ResponseEntity<String> notice(HttpServletRequest request,
                                         @PathVariable("payType") Integer payType,
                                         @PathVariable("payEntry") Integer payEntry,
                                         @RequestBody(required = false) String data) throws WxPayException, UnsupportedEncodingException, AlipayApiException {

        PayInfoResultBO payInfoResultBO = payManager.validateAndGetPayInfo(request, PayType.instance(payType), data);

        // 校验订单参数异常，返回未授权
        if (!payInfoResultBO.getPaySuccess()) {
            return ResponseEntity.ok(payInfoResultBO.getSuccessString());
        }

        PayInfo payInfo = payInfoService.getByPayId(payInfoResultBO.getPayId());
        // 已经支付
        if (Objects.equals(payInfo.getPayStatus(), PayStatus.PAYED.value()) || Objects.equals(payInfo.getPayStatus(), PayStatus.REFUND.value())) {
            return ResponseEntity.ok(payInfoResultBO.getSuccessString());
        }
        // 支付金额不对
        if (!Objects.equals(payInfo.getPayAmount(), payInfoResultBO.getPayAmount())) {
            return ResponseEntity.ok(ResponseEnum.UNAUTHORIZED.value());
        }
        if (Objects.equals(payEntry, PayEntry.ORDER.value())) {
            // 订单支付回调
            return payNoticeManager.noticeOrder(payInfoResultBO, payInfo);
        } else if (Objects.equals(payEntry, PayEntry.RECHARGE.value())) {
            // 余额充值回调
            return payNoticeManager.noticeRecharge(payInfoResultBO, payInfo);
        } else if (Objects.equals(payEntry, PayEntry.VIP.value())) {
            // 购买会员回调
            return payNoticeManager.noticeBuyVip(payInfoResultBO, payInfo);
        }
        return ResponseEntity.ok(ResponseEnum.UNAUTHORIZED.value());
    }

}
